/*
 * =====================================================================================
 *
 *       Filename:  maxThickness.cpp
 *
 *    Description:  Returns maximum segment thickness over x and y
 *
 *        Version:  1.0
 *        Created:  05/07/12 11:34:28
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Moos Hueting (mh), mooshueting@gmail.com
 *   Organization:  
 *
 * =====================================================================================
 */
#include <iostream>
#include <cv.h>
#include <highgui.h>

using namespace std;
using namespace cv;

int avgThickness(Mat src, char rowcol) {
    Size s = src.size();

    int thickness;
    bool inSegment;
    int nSegments = 0;
    float avgThickness = 0.0;

    if (rowcol == 0) {
        // find average thickness per row
        for (int i=0; i < s.height; i++) {
            inSegment = false;
            thickness = 0;
            // iterate over all columns
            for (int j=0; j < s.width; j++) {
                // if the current pixel is white
                if (src.at<unsigned char>(i,j) != 0) {
                    // increase the thickness of the current segment by 1
                    thickness++;
                    // we are now in a segment
                    inSegment = true;
                // if the current pixel is black
                } else {
                    // if it's the first black pixel
                    if (inSegment) {
                        // if the last segment was thicker than the thickest till now
                        if (thickness > maxRowThickness)
                            maxRowThickness = thickness;
                        // we are no longer in a segment
                        inSegment = false;
                    }
                }
            }
        }
        return maxRowThickness;
    } else {
        int maxColThickness = 0;
        // find maximum thickness per column
        for (int i=0; i < s.width; i++) {
            inSegment = false;
            thickness = 0;
            // iterate over all rows 
            for (int j=0; j < s.height; j++) {
                // if the current pixel is white
                if (src.at<unsigned char>(j,i) != 0) {
                    // increase the thickness of the current segment by 1
                    thickness++;
                    // we are now in a segment
                    inSegment = true;
                // if the current pixel is black
                } else {
                    // if it's the first black pixel
                    if (inSegment) {
                        // if the last segment was thicker than the thickest till now
                        if (thickness > maxColThickness)
                            maxColThickness = thickness;
                        // we are no longer in a segment
                        inSegment = false;
                    }
                }
            }
        }
        return maxColThickness;
    }
}
